from metagpt.roles import Role
from actions import AnalyzeTopic, CreateOutline, WriteContent, ReviewContent


class TopicAnalyzer(Role):
    """话题分析角色，负责分析文章主题"""
    
    def __init__(self, name="话题分析师", profile="专注于分析文章主题，确定目标受众和内容方向"):
        super().__init__(name=name, profile=profile)
        self.set_action(AnalyzeTopic())
        
    async def run(self, requirements: str):
        """运行话题分析"""
        return await self.actions[0].run(requirements)


class OutlineCreator(Role):
    """大纲创建角色，负责创建文章大纲"""
    
    def __init__(self, name="大纲设计师", profile="专注于创建结构清晰的文章大纲"):
        super().__init__(name=name, profile=profile)
        self.set_action(CreateOutline())
        
    async def run(self, analysis):
        """运行大纲创建"""
        return await self.actions[0].run(analysis)


class ContentWriter(Role):
    """内容创作角色，负责撰写文章内容"""
    
    def __init__(self, name="内容撰写员", profile="专注于根据大纲撰写高质量的文章内容"):
        super().__init__(name=name, profile=profile)
        self.set_action(WriteContent())
        
    async def run(self, analysis, outline):
        """运行内容撰写"""
        return await self.actions[0].run(analysis, outline)


class ContentReviewer(Role):
    """内容审查角色，负责审查和优化文章"""
    
    def __init__(self, name="内容审核员", profile="专注于审查文章内容并提供改进建议"):
        super().__init__(name=name, profile=profile)
        self.set_action(ReviewContent())
        
    async def run(self, analysis, content):
        """运行内容审查"""
        return await self.actions[0].run(analysis, content)


class ArticleManager(Role):
    """文章管理角色，负责协调整个创作过程"""
    
    def __init__(self, name="文章项目经理", profile="协调整个文章创作流程，保证质量和一致性"):
        super().__init__(name=name, profile=profile)
        self.topic_analyzer = TopicAnalyzer()
        self.outline_creator = OutlineCreator()
        self.content_writer = ContentWriter()
        self.content_reviewer = ContentReviewer()
        
    async def run(self, topic_requirements: str):
        """运行整个文章创作流程"""
        # 步骤1：分析主题
        print(f"📊 {self.topic_analyzer.name}正在分析主题...")
        analysis = await self.topic_analyzer.run(topic_requirements)
        print(f"✅ 主题分析完成")
        
        # 步骤2：创建大纲
        print(f"📝 {self.outline_creator.name}正在创建大纲...")
        outline = await self.outline_creator.run(analysis)
        print(f"✅ 大纲创建完成")
        
        # 步骤3：撰写内容
        print(f"✍️ {self.content_writer.name}正在撰写内容...")
        content = await self.content_writer.run(analysis, outline)
        print(f"✅ 内容撰写完成")
        
        # 步骤4：审查内容
        print(f"🔍 {self.content_reviewer.name}正在审查内容...")
        review = await self.content_reviewer.run(analysis, content)
        print(f"✅ 内容审查完成")
        
        # 返回完整结果
        return {
            "analysis": analysis,
            "outline": outline,
            "content": content,
            "review": review
        } 